package com.yc.fav.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yc.fav.entity.Tag;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author LiaoYan
 * @since 2025-08-16
 */
public interface TagMapper extends BaseMapper<Tag> {

    //根据用户id查询标签
    @Select("SELECT\n" +
            "	*\n" +
            "FROM\n" +
            "	tag a\n" +
            "WHERE\n" +
            "	EXISTS (\n" +
            "		SELECT\n" +
            "			*\n" +
            "		FROM\n" +
            "			tag_fav b\n" +
            "		JOIN fav c ON b.fid = c.id\n" +
            "		WHERE\n" +
            "			a.id = b.tid\n" +
            "		AND c.uid = #{uid}\n" +
            "	)")
    List<Tag> selectByUid(int uid);


    @Select("select tag.*, #{uid} uid from tag where id=#{tid}")
    // <resultMap><id><result><collection property="favs" column="id">...</resultMap>
    @Results({
            @Result(column = "id", property = "id", id = true), //<id>
            @Result(column = "name", property = "name"), //<result>
            @Result(column = "{tid=id , uid=uid}", property = "favs",
//                    one = ..  一对一  many 一对多
                    many = @Many(select = "com.yc.fav.mapper.FavMapper.queryByTidAndUid"))
    })
    Tag queryById(@Param("tid") int tid, @Param("uid")int uid);
}
